From ddec0431076e640caa6c77ef44d72c4aa9949c16 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Sat, 14 Apr 2018 10:49:15 +0200 Subject: [PATCH] Store compile mode as enum in BuildConfig --- src/bin/cargo/command_prelude.rs | 2 +- src/cargo/core/compiler/context/mod.rs | 3 +- .../compiler/context/unit_dependencies.rs | 5 +- src/cargo/core/compiler/job_queue.rs | 3 +- src/cargo/core/compiler/mod.rs | 96 ++++++++++++++++++- src/cargo/core/profiles.rs | 2 +- src/cargo/ops/cargo_clean.rs | 7 +- src/cargo/ops/cargo_compile.rs | 89 +---------------- src/cargo/ops/cargo_fetch.rs | 5 +- src/cargo/ops/cargo_package.rs | 4 +- src/cargo/ops/mod.rs | 2 +- 11 files changed, 110 insertions(+), 108 deletions(-) diff --git a/src/bin/cargo/command_prelude.rs b/src/bin/cargo/command_prelude.rs index 3f98f9ea4..fb1b907a4 100644 --- a/src/bin/cargo/command_prelude.rs +++ b/src/bin/cargo/command_prelude.rs @@ -11,7 +11,7 @@ use cargo::util::important_paths::find_root_manifest_for_wd; pub use clap::{AppSettings, Arg, ArgMatches}; pub use cargo::{CliError, CliResult, Config}; -pub use cargo::ops::CompileMode; +pub use cargo::core::compiler::CompileMode; pub type App = clap::App<'static, 'static>; diff --git a/src/cargo/core/compiler/context/mod.rs b/src/cargo/core/compiler/context/mod.rs index a0e166860..02b6f1111 100644 --- a/src/cargo/core/compiler/context/mod.rs +++ b/src/cargo/core/compiler/context/mod.rs @@ -8,7 +8,6 @@ use jobserver::Client; use core::{Package, PackageId, Resolve, Target}; use core::profiles::Profile; -use ops::CompileMode; use util::errors::{CargoResult, CargoResultExt}; use util::{internal, profile, Config}; @@ -16,7 +15,7 @@ use super::custom_build::{self, BuildDeps, BuildScripts, BuildState}; use super::fingerprint::Fingerprint; use super::job_queue::JobQueue; use super::layout::Layout; -use super::{BuildContext, Compilation, Executor, FileFlavor, Kind}; +use super::{BuildContext, Compilation, CompileMode, Executor, FileFlavor, Kind}; mod unit_dependencies; use self::unit_dependencies::build_unit_dependencies; diff --git a/src/cargo/core/compiler/context/unit_dependencies.rs b/src/cargo/core/compiler/context/unit_dependencies.rs index ee66f62d9..e53c3da37 100644 --- a/src/cargo/core/compiler/context/unit_dependencies.rs +++ b/src/cargo/core/compiler/context/unit_dependencies.rs @@ -15,11 +15,10 @@ //! (for example, with and without tests), so we actually build a dependency //! graph of `Unit`s, which capture these properties. -use super::{BuildContext, Kind, Unit}; +use super::{BuildContext, CompileMode, Kind, Unit}; use core::dependency::Kind as DepKind; use core::profiles::ProfileFor; use core::{Package, Target}; -use ops::CompileMode; use std::collections::HashMap; use CargoResult; @@ -35,7 +34,7 @@ pub fn build_unit_dependencies<'a, 'cfg>( // cleared, and avoid building the lib thrice (once with `panic`, once // without, once for --test). In particular, the lib included for // doctests and examples are `Build` mode here. - let profile_for = if unit.mode.is_any_test() || bcx.build_config.test { + let profile_for = if unit.mode.is_any_test() || bcx.build_config.test() { ProfileFor::TestDependency } else { ProfileFor::Any diff --git a/src/cargo/core/compiler/job_queue.rs b/src/cargo/core/compiler/job_queue.rs index f167b49de..235dccc93 100644 --- a/src/cargo/core/compiler/job_queue.rs +++ b/src/cargo/core/compiler/job_queue.rs @@ -11,12 +11,11 @@ use jobserver::{Acquired, HelperThread}; use core::profiles::Profile; use core::{PackageId, Target}; use handle_error; -use ops::CompileMode; use util::{internal, profile, CargoResult, CargoResultExt, ProcessBuilder}; use util::{Config, DependencyQueue, Dirty, Fresh, Freshness}; use super::job::Job; -use super::{BuildContext, Context, Kind, Unit}; +use super::{BuildContext, CompileMode, Context, Kind, Unit}; /// A management structure of the entire dependency graph to compile. /// diff --git a/src/cargo/core/compiler/mod.rs b/src/cargo/core/compiler/mod.rs index 82cbbc43d..031321faf 100644 --- a/src/cargo/core/compiler/mod.rs +++ b/src/cargo/core/compiler/mod.rs @@ -12,7 +12,6 @@ use serde_json; use core::profiles::{Lto, Profile}; use core::shell::ColorChoice; use core::{Feature, PackageId, Target}; -use ops::CompileMode; use util::errors::{CargoResult, CargoResultExt, Internal}; use util::paths; use util::{self, machine_message, Config, Freshness, ProcessBuilder, Rustc}; @@ -61,8 +60,8 @@ pub struct BuildConfig { pub jobs: u32, /// Whether we are building for release pub release: bool, - /// Whether we are running tests - pub test: bool, + /// In what mode we are compiling + pub mode: CompileMode, /// Whether to print std output in json format (for machine reading) pub message_format: MessageFormat, } @@ -81,6 +80,7 @@ impl BuildConfig { jobs: Option, requested_target: &Option, rustc_info_cache: Option, + mode: CompileMode, ) -> CargoResult { if let &Some(ref s) = requested_target { if s.trim().is_empty() { @@ -131,7 +131,7 @@ impl BuildConfig { host: host_config, target: target_config, release: false, - test: false, + mode, message_format: MessageFormat::Human, }) } @@ -156,6 +156,10 @@ impl BuildConfig { pub fn json_messages(&self) -> bool { self.message_format == MessageFormat::Json } + + pub fn test(&self) -> bool { + self.mode == CompileMode::Test || self.mode == CompileMode::Bench + } } #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -164,6 +168,90 @@ pub enum MessageFormat { Json, } +/// The general "mode" of what to do. +/// This is used for two purposes. The commands themselves pass this in to +/// `compile_ws` to tell it the general execution strategy. This influences +/// the default targets selected. The other use is in the `Unit` struct +/// to indicate what is being done with a specific target. +#[derive(Clone, Copy, PartialEq, Debug, Eq, Hash)] +pub enum CompileMode { + /// A target being built for a test. + Test, + /// Building a target with `rustc` (lib or bin). + Build, + /// Building a target with `rustc` to emit `rmeta` metadata only. If + /// `test` is true, then it is also compiled with `--test` to check it like + /// a test. + Check { test: bool }, + /// Used to indicate benchmarks should be built. This is not used in + /// `Target` because it is essentially the same as `Test` (indicating + /// `--test` should be passed to rustc) and by using `Test` instead it + /// allows some de-duping of Units to occur. + Bench, + /// A target that will be documented with `rustdoc`. + /// If `deps` is true, then it will also document all dependencies. + Doc { deps: bool }, + /// A target that will be tested with `rustdoc`. + Doctest, + /// A marker for Units that represent the execution of a `build.rs` + /// script. + RunCustomBuild, +} + +impl CompileMode { + /// Returns true if the unit is being checked. + pub fn is_check(&self) -> bool { + match *self { + CompileMode::Check { .. } => true, + _ => false, + } + } + + /// Returns true if this is a doc or doctest. Be careful using this. + /// Although both run rustdoc, the dependencies for those two modes are + /// very different. + pub fn is_doc(&self) -> bool { + match *self { + CompileMode::Doc { .. } | CompileMode::Doctest => true, + _ => false, + } + } + + /// Returns true if this is any type of test (test, benchmark, doctest, or + /// check-test). + pub fn is_any_test(&self) -> bool { + match *self { + CompileMode::Test + | CompileMode::Bench + | CompileMode::Check { test: true } + | CompileMode::Doctest => true, + _ => false, + } + } + + /// Returns true if this is the *execution* of a `build.rs` script. + pub fn is_run_custom_build(&self) -> bool { + *self == CompileMode::RunCustomBuild + } + + /// List of all modes (currently used by `cargo clean -p` for computing + /// all possible outputs). + pub fn all_modes() -> &'static [CompileMode] { + static ALL: [CompileMode; 9] = [ + CompileMode::Test, + CompileMode::Build, + CompileMode::Check { test: true }, + CompileMode::Check { test: false }, + CompileMode::Bench, + CompileMode::Doc { deps: true }, + CompileMode::Doc { deps: false }, + CompileMode::Doctest, + CompileMode::RunCustomBuild, + ]; + &ALL + } +} + /// Information required to build for a target #[derive(Clone, Default)] pub struct TargetConfig { diff --git a/src/cargo/core/profiles.rs b/src/cargo/core/profiles.rs index fbca991d6..8ad40abd8 100644 --- a/src/cargo/core/profiles.rs +++ b/src/cargo/core/profiles.rs @@ -1,9 +1,9 @@ use std::collections::HashSet; use std::{cmp, fmt, hash}; +use core::compiler::CompileMode; use core::interning::InternedString; use core::Shell; -use ops::CompileMode; use util::lev_distance::lev_distance; use util::toml::{StringOrBool, TomlProfile, U32OrBool}; use util::CargoResult; diff --git a/src/cargo/ops/cargo_clean.rs b/src/cargo/ops/cargo_clean.rs index f9f032760..6d5a2b7d9 100644 --- a/src/cargo/ops/cargo_clean.rs +++ b/src/cargo/ops/cargo_clean.rs @@ -1,10 +1,10 @@ use std::fs; use std::path::Path; -use core::compiler::{BuildConfig, BuildContext, Context, Kind, Unit}; +use core::compiler::{BuildConfig, BuildContext, CompileMode, Context, Kind, Unit}; use core::profiles::ProfileFor; use core::Workspace; -use ops::{self, CompileMode}; +use ops; use util::errors::{CargoResult, CargoResultExt}; use util::paths; use util::Config; @@ -88,7 +88,8 @@ pub fn clean(ws: &Workspace, opts: &CleanOptions) -> CargoResult<()> { } } - let mut build_config = BuildConfig::new(config, Some(1), &opts.target, None)?; + let mut build_config = + BuildConfig::new(config, Some(1), &opts.target, None, CompileMode::Build)?; build_config.release = opts.release; let bcx = BuildContext::new( ws, diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index 87c37b0fe..0abad64f0 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -27,7 +27,7 @@ use std::path::{Path, PathBuf}; use std::sync::Arc; use core::compiler::{BuildConfig, BuildContext, Compilation, Context, DefaultExecutor, Executor}; -use core::compiler::{Kind, MessageFormat, Unit}; +use core::compiler::{CompileMode, Kind, MessageFormat, Unit}; use core::profiles::{ProfileFor, Profiles}; use core::resolver::{Method, Resolve}; use core::{Package, Source, Target}; @@ -97,90 +97,6 @@ impl<'a> CompileOptions<'a> { } } -/// The general "mode" of what to do. -/// This is used for two purposes. The commands themselves pass this in to -/// `compile_ws` to tell it the general execution strategy. This influences -/// the default targets selected. The other use is in the `Unit` struct -/// to indicate what is being done with a specific target. -#[derive(Clone, Copy, PartialEq, Debug, Eq, Hash)] -pub enum CompileMode { - /// A target being built for a test. - Test, - /// Building a target with `rustc` (lib or bin). - Build, - /// Building a target with `rustc` to emit `rmeta` metadata only. If - /// `test` is true, then it is also compiled with `--test` to check it like - /// a test. - Check { test: bool }, - /// Used to indicate benchmarks should be built. This is not used in - /// `Target` because it is essentially the same as `Test` (indicating - /// `--test` should be passed to rustc) and by using `Test` instead it - /// allows some de-duping of Units to occur. - Bench, - /// A target that will be documented with `rustdoc`. - /// If `deps` is true, then it will also document all dependencies. - Doc { deps: bool }, - /// A target that will be tested with `rustdoc`. - Doctest, - /// A marker for Units that represent the execution of a `build.rs` - /// script. - RunCustomBuild, -} - -impl CompileMode { - /// Returns true if the unit is being checked. - pub fn is_check(&self) -> bool { - match *self { - CompileMode::Check { .. } => true, - _ => false, - } - } - - /// Returns true if this is a doc or doctest. Be careful using this. - /// Although both run rustdoc, the dependencies for those two modes are - /// very different. - pub fn is_doc(&self) -> bool { - match *self { - CompileMode::Doc { .. } | CompileMode::Doctest => true, - _ => false, - } - } - - /// Returns true if this is any type of test (test, benchmark, doctest, or - /// check-test). - pub fn is_any_test(&self) -> bool { - match *self { - CompileMode::Test - | CompileMode::Bench - | CompileMode::Check { test: true } - | CompileMode::Doctest => true, - _ => false, - } - } - - /// Returns true if this is the *execution* of a `build.rs` script. - pub fn is_run_custom_build(&self) -> bool { - *self == CompileMode::RunCustomBuild - } - - /// List of all modes (currently used by `cargo clean -p` for computing - /// all possible outputs). - pub fn all_modes() -> &'static [CompileMode] { - static ALL: [CompileMode; 9] = [ - CompileMode::Test, - CompileMode::Build, - CompileMode::Check { test: true }, - CompileMode::Check { test: false }, - CompileMode::Bench, - CompileMode::Doc { deps: true }, - CompileMode::Doc { deps: false }, - CompileMode::Doctest, - CompileMode::RunCustomBuild, - ]; - &ALL - } -} - #[derive(Clone, PartialEq, Eq, Debug)] pub enum Packages { Default, @@ -332,9 +248,8 @@ pub fn compile_ws<'a>( let rustc_info_cache = ws.target_dir() .join(".rustc_info.json") .into_path_unlocked(); - let mut build_config = BuildConfig::new(config, jobs, &target, Some(rustc_info_cache))?; + let mut build_config = BuildConfig::new(config, jobs, &target, Some(rustc_info_cache), mode)?; build_config.release = release; - build_config.test = mode == CompileMode::Test || mode == CompileMode::Bench; build_config.message_format = message_format; let default_arch_kind = if build_config.requested_target.is_some() { Kind::Target diff --git a/src/cargo/ops/cargo_fetch.rs b/src/cargo/ops/cargo_fetch.rs index db400fb4d..0bf64ba19 100644 --- a/src/cargo/ops/cargo_fetch.rs +++ b/src/cargo/ops/cargo_fetch.rs @@ -1,4 +1,4 @@ -use core::compiler::{BuildConfig, Kind, TargetInfo}; +use core::compiler::{BuildConfig, CompileMode, Kind, TargetInfo}; use core::{PackageSet, Resolve, Workspace}; use ops; use std::collections::HashSet; @@ -19,7 +19,8 @@ pub fn fetch<'a>( let (packages, resolve) = ops::resolve_ws(ws)?; let jobs = Some(1); - let build_config = BuildConfig::new(ws.config(), jobs, &options.target, None)?; + let build_config = + BuildConfig::new(ws.config(), jobs, &options.target, None, CompileMode::Build)?; let target_info = TargetInfo::new(ws.config(), &build_config, Kind::Target)?; { let mut fetched_packages = HashSet::new(); diff --git a/src/cargo/ops/cargo_package.rs b/src/cargo/ops/cargo_package.rs index 1cb3f24fb..cf898b30c 100644 --- a/src/cargo/ops/cargo_package.rs +++ b/src/cargo/ops/cargo_package.rs @@ -10,7 +10,7 @@ use git2; use tar::{Archive, Builder, EntryType, Header}; use core::{Package, Source, SourceId, Workspace}; -use core::compiler::{DefaultExecutor, MessageFormat}; +use core::compiler::{CompileMode, DefaultExecutor, MessageFormat}; use sources::PathSource; use util::{self, internal, Config, FileLock}; use util::paths; @@ -348,7 +348,7 @@ fn run_verify(ws: &Workspace, tar: &FileLock, opts: &PackageOpts) -> CargoResult }, release: false, message_format: MessageFormat::Human, - mode: ops::CompileMode::Build, + mode: CompileMode::Build, target_rustdoc_args: None, target_rustc_args: None, export_dir: None, diff --git a/src/cargo/ops/mod.rs b/src/cargo/ops/mod.rs index 08e65ba54..1d0619361 100644 --- a/src/cargo/ops/mod.rs +++ b/src/cargo/ops/mod.rs @@ -1,6 +1,6 @@ pub use self::cargo_clean::{clean, CleanOptions}; pub use self::cargo_compile::{compile, compile_with_exec, compile_ws, CompileOptions}; -pub use self::cargo_compile::{CompileFilter, CompileMode, FilterRule, Packages}; +pub use self::cargo_compile::{CompileFilter, FilterRule, Packages}; pub use self::cargo_read_manifest::{read_package, read_packages}; pub use self::cargo_run::run; pub use self::cargo_install::{install, install_list, uninstall}; -- 2.30.2